package com.loie.datacenter.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.loie.datacenter.comm.CommJSON;
import com.loie.datacenter.model.bo.*;
import com.loie.datacenter.service.UserListService;
import com.loie.datacenter.util.SecureUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhl
 * @create 2021/9/11 19:55
 */
@Slf4j
@RequestMapping("/userList")
@RestController
@AllArgsConstructor
public class UserListController {

    private final UserListService userListService;
    private final RedisTemplate<String, String> redisTemplate;


    @PostMapping("/getOriginUserList")
    public
    CommJSON getOriginUserList(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            ParamBO bo = JSONObject.parseObject(mwMsg, ParamBO.class);

            List<OriginUserBO> originUserBOList = userListService.getOriginUserList(bo);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total", userListService.getOriginUserListCount(bo));
            jsonObject.put("list", originUserBOList);

            commJson.setSuc();
            commJson.setData(SecureUtil.encryptAES(jsonObject.toJSONString(), aesSecureKey));
        } catch (Exception e) {
            commJson.setErrorMessage("获取原用户列表异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.getOriginUserList()===>", e);
        }
        return commJson;
    }

    @PostMapping("/getUserList")
    public
    CommJSON getUserList(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            UserListBO bo = JSONObject.parseObject(mwMsg, UserListBO.class);

            List<UserListBO> dataList = userListService.getUserList(bo);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total", userListService.getUserListCount(bo));
            jsonObject.put("list", dataList);

            commJson.setSuccess(true);
            commJson.setData(SecureUtil.encryptAES(jsonObject.toJSONString(), aesSecureKey));
        } catch (Exception e) {
            commJson.setErrorMessage("获取用户列表异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.getUserList()===>", e);
        }
        return commJson;
    }

    @PostMapping("/addUser")
    public
    CommJSON addUser(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        return updateUser(msg, tokenInfoBO);
    }

    @PostMapping("/updateUser")
    public
    CommJSON updateUser(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            UserListBO bo = JSONObject.parseObject(mwMsg, UserListBO.class);

            if (userListService.isExist(bo)) {
                commJson.setErrorMessage("用户已存在");
                commJson.setError();
            } else {
                userListService.updateUserList(bo);
                commJson.setSuc();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", bo.getId());
                jsonObject.put("createdDate", bo.getCreatedDate());
                commJson.setData(SecureUtil.encryptAES(jsonObject.toJSONString(), aesSecureKey));
            }
        } catch (Exception e) {
            commJson.setErrorMessage("更新用户异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.updateUser()===>", e);
        }
        return commJson;
    }

    @PostMapping("/deleteUser")
    public
    CommJSON deleteUser(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            UserListBO bo = JSONObject.parseObject(mwMsg, UserListBO.class);

            userListService.deleteUserList(bo);
            commJson.setSuc();
        } catch (Exception e) {
            commJson.setErrorMessage("删除用户异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.deleteUser()===>", e);
        }
        return commJson;
    }

    @PostMapping("/setIsLocked")
    public
    CommJSON setIsLocked(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            UserListBO bo = JSONObject.parseObject(mwMsg, UserListBO.class);
            userListService.setIsLocked(bo.getId(), bo.getIsLocked());
            commJson.setSuc();
        } catch (Exception e) {
            commJson.setErrorMessage("设置禁用状态异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.setIsLocked()===>", e);
        }
        return commJson;
    }

    @PostMapping("/reset")
    public
    CommJSON reset(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO ) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            LoginBO bo = JSONObject.parseObject(mwMsg, LoginBO.class);
            userListService.updatePassword(bo.getId(), bo.getNewPassword());
            commJson.setSuc();
        } catch (Exception e) {
            commJson.setErrorMessage("重置密码异常");
            commJson.setError();
            e.printStackTrace();
            log.error("UserListController.reset()===>", e);
        }
        return commJson;
    }
}
